home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / cpptask.exe / TSIO.CPP < prev    next >
C/C++ Source or Header  |  1991-08-20  |  3KB  |  131 lines

  1.  
  2. /*
  3.    Test program for checking the CTask serial I/O interface.
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <conio.h>
  8. #include <ctype.h>
  9. #include <process.h>
  10.  
  11. #include "task.hpp"
  12. #include "tsklocal.hpp"
  13. #include "sio.hpp"
  14.  
  15. #define COM1PORT 0x80     /* COM1, relative */
  16. #define COM2PORT 0x81     /* COM2, relative */
  17. #define BAUD     9600L    /* Baudrate */
  18.  
  19. #define STACKSIZE 2048
  20.  
  21. unsigned int _stklen = 3 * STACKSIZE;  /* Two tasks + main Task Stack */
  22.  
  23. void far task1 (void);
  24. local char stack1 [STACKSIZE];
  25. local task tcb1((funcptr)task1, (byteptr)stack1, STACKSIZE, PRI_STD, NULL);
  26.  
  27. void far task2 (void);
  28. local char stack2 [STACKSIZE];
  29. local task tcb2((funcptr)task2, (byteptr)stack2, STACKSIZE, PRI_STD, NULL);
  30.  
  31. flag halt;
  32.  
  33. word rcvbuf [10000];    // receive buffer for serial port
  34. byte xmtbuf [100];      // transmit buffer for serial port
  35.  
  36. commport com1(COM1PORT, 1, rcvbuf, sizeof(rcvbuf),  // create com1 port
  37.                            xmtbuf, sizeof(xmtbuf));
  38.  
  39. int endrun, err;
  40.  
  41. /*
  42.    Task 1 reads characters from the serial line and displays them on
  43.    the screen. While the halt flag is set, characters are not read,
  44.    so the XON/XOFF and RTS/CTS protocol can be tested for the receiving 
  45.    side.
  46. */
  47.  
  48. void far task1 (void)
  49. {
  50.    word ch;
  51.  
  52.    printf ("Task 1 started\n");
  53.    while (!endrun)
  54.       {
  55.       halt.wait_flag_clear (0L);
  56.       if (endrun)
  57.          return;
  58.       ch = com1.receive (0L);
  59.       putch (ch);
  60.       if (ch & 0xff00)
  61.          {
  62.          err = 1;
  63.          printf ("\n%c*%02x*", ch, ch >> 8);
  64.          }
  65.       }
  66. }
  67.  
  68.  
  69. /*
  70.    Task 2 reads characters from the keyboard and sends them to the
  71.    serial port. If 'h' is entered, the halt flag is set, so task1
  72.    stops reading. If 'c' is entered, the halt flag is cleared.
  73.    Entering 'e' stops the program.
  74. */
  75.  
  76. void far task2 (void)
  77. {
  78.    int ch;
  79.  
  80.    printf ("Task 2 started\n");
  81.    while (!endrun)
  82.       {
  83.       ch = t_read_key () & 0xff;
  84.       switch (tolower (ch))
  85.          {
  86.          case 'h':   halt.set_flag ();
  87.                      puts ("-halt-");
  88.                      break;
  89.  
  90.          case 'c':   halt.clear_flag ();
  91.                      err = 0;
  92.                      puts ("-continue-");
  93.                      break;
  94.  
  95.          case 'e':   puts ("-end-");
  96.                      endrun = 1;
  97.                      halt.clear_flag ();
  98.                      main_tcb.wake_task ();
  99.                      break;
  100.  
  101.          default:    com1.send (ch, 0L);
  102.                      break;
  103.          }
  104.       }
  105. }
  106.  
  107.  
  108. main ()
  109. {
  110.    endrun = 0;
  111.  
  112.    com1.change_baud (BAUD);
  113.  
  114.    tcb1.start_task ();
  115.    tcb2.start_task ();
  116.  
  117.    tasker.preempt_on ();
  118.    t_delay (0L);
  119.  
  120.    endrun = 1;
  121.    puts ("******** Main Task *********");
  122.  
  123.    main_tcb.set_priority (10);
  124.    tasker.schedule ();
  125.    tasker.preempt_off ();
  126.  
  127.    puts ("******** End Run *********");
  128.    return 0;
  129. }
  130.  
  131.